﻿using BLL;
using DAL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Model.medical;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace HotelCode.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MedicationController : ControllerBase
    {
        public MedicationBLL MedicationBLL { get; }

        public MedicationController(MedicationBLL medicationBLL)
        {
            MedicationBLL = medicationBLL;
        }
        [HttpPost]
        /// <summary>
        /// 用药添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public IActionResult MedicaAdd(MedicationModel info)
        {
            try
            {
                int res= MedicationBLL.MedicaAdd(info);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
       
        [HttpGet]
        /// <summary>
        /// Id查询反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult GetMedicationId(int id)
        {
            try
            {
                var list= MedicationBLL.GetMedicationId(id);
                return Ok(list);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpDelete]
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult MedicaDelete(int id)
        {
            try
            {
                int res= MedicationBLL.MedicaDelete(id);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpPut]
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public IActionResult MedicaUptAll(string ids, int state)
        {
            try
            {
                int res= MedicationBLL.MedicaUptAll(ids,state);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpPut]
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public IActionResult MedicaUpt(MedicationModel info)
        {
            try
            {
                int res= MedicationBLL.MedicaUpt(info);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="totalCount"></param>
        /// <param name="totalPage"></param>
        /// <param name="Pageindex"></param>
        /// <param name="pageSize"></param>
        /// <param name="Dispensingdoctors"></param>
        /// <param name="DepartId"></param>
        /// <returns></returns>
        public IActionResult GetMedicatioList(int Pageindex = 1, int pageSize = 2, string? Dispensingdoctors = "", int? DepartId = null)
        {
            try
            {
                int totalCount = 0; int totalPage = 0;
                var list = MedicationBLL.GetMedicatioList(out totalCount, out totalPage, Pageindex, pageSize, Dispensingdoctors, DepartId);
                return Ok(new
                {
                    list = list,
                    totalCount = totalCount,
                    totalPage = totalPage,
                });
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        public IActionResult Exslsxinmp(int Pageindex = 1, int pageSize = 2, string? Dispensingdoctors = "", int? DepartId = null)
        {
            try
            {
                byte[] data = null;
                string t = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                string f = "用药管理.xlsx";
                IWorkbook b= new XSSFWorkbook();
                ISheet s=b.CreateSheet();
                IRow row1=s.CreateRow(0);
                row1.CreateCell(0).SetCellValue("药品名称");
                row1.CreateCell(1).SetCellValue("科室");
                row1.CreateCell(2).SetCellValue("用药日期");
                row1.CreateCell(3).SetCellValue("病房号码");
                row1.CreateCell(4).SetCellValue("病人姓名");
                row1.CreateCell(5).SetCellValue("配药医生");
                int totalCount = 0; int totalPage = 0;
                var list = MedicationBLL.GetMedicatioList(out totalCount, out totalPage, Pageindex, pageSize, Dispensingdoctors, DepartId);
                int Rowindex = 1;
                foreach (var item in list)
                {
                    IRow row = s.CreateRow(Rowindex);
                    row.CreateCell(0).SetCellValue(item.MedicalName);
                    row.CreateCell(1).SetCellValue(item.DepartId);
                    row.CreateCell(2).SetCellValue(Convert.ToDateTime(item.Medicatime));
                    row.CreateCell(3).SetCellValue(item.patientNumber);
                    row.CreateCell(4).SetCellValue(item.patientName);
                    row.CreateCell(5).SetCellValue(item.Dispensingdoctors);
                    Rowindex++;
                }
                using (MemoryStream ms=new MemoryStream())
                {
                    b.Write(ms);
                    data = ms.ToArray();
                }
                return File(data, t, f);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
